﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.OMM.HealthMonitor.Collector.SharedHealthDataMemoryStorage.HftProcessor;
using Sinacor.OMM.HealthMonitor.Collector;
using Sinacor.OMM.HealthMonitor.Entity.HftProcessor;
using System.Threading;
using Sinacor.OMM.HealthMonitor.Entity.Common;
using Sinacor.OMM.HealthMonitor.Configuration;

namespace Sinacor.OMM.HealthMonitor.Server.Controller
{
    public class HsCtrlHftProcessesShdms
    {
        public static SerializableDictionary<string, HftProcessorData> CheckExpiration(SerializableDictionary<string, HftProcessorData> dictionary)
        {
            int secondsToExpire = OMM.HealthMonitor.Configuration.HealthMonitorConfigurationManager.HealthMonitorConfiguration.TimeToRefreshMonitorSeconds * 5;
            
            double timeElaspseSinceLastUpdate = 0;

            if (dictionary != null)
            {
                lock (dictionary)
                {
                    if (dictionary.Count > 0)
                    {
                        List<string> listKeys = new List<string>();
                        foreach (string key in dictionary.Keys)
                        {
                            listKeys.Add(key);
                        }

                        foreach (string key in listKeys)
                        {
                            timeElaspseSinceLastUpdate = DateTime.Now.Subtract(dictionary[key].LastUpdate).TotalSeconds;
                            if (timeElaspseSinceLastUpdate >= secondsToExpire)
                            {
                                dictionary.Remove(key);
                            }
                        }
                        listKeys.Clear();
                        listKeys = null;
                    }
                }
            }

            return dictionary;
        }
    }
}
